<template>
  <div v-loading="loading">
    <el-row :gutter="10">
      <el-col
        :span="12"
        style="text-align: left;"
      >
        <el-button
          type="success"
          @click="addBtnClick"
        >添加</el-button>
        <el-input
          style="display:inline;margin: 0px 12px"
          v-model="
          tableDatas.filters.searchText"
        >
          <template #prepend>搜索</template>
        </el-input>
        <el-button
          type="success"
          @click="exportBtnClick"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-dialog
      v-model="dialogDatas.dialogFormVisible"
      :title="dialogDatas.isNew ? '添加':'修改'"
      top="5vh"
    >
      <el-scrollbar max-height="70vh">
        <el-form :model="dialogDatas.currentData">
          <el-form-item
            v-for="item in dialogDatas.formItems"
            :label="item.label"
          >
            <div v-if="item.isDate">
              <el-date-picker
                v-model="dialogDatas.currentData[item.value]"
                :type="item.dateOption ? item.dateOption.type : 'date'"
                :format="item.dateOption ? item.dateOption.format : 'YYYY/MM/DD'"
                :value-format="item.dateOption ? item.dateOption.valueFormat : 'YYYY/MM/DD'"
                :date-format="item.dateOption ? item.dateOption.dateFormat : ''"
                :time-format="item.dateOption ? item.dateOption.timeFormat : ''"
                :disabled="item.disable"
              />
            </div>

            <div
              v-else-if="item.isSelect"
              style="width: 100%"
            >
              <el-select
                v-model="dialogDatas.currentData[item.value]"
                :disabled="item.disable"
                filterable
              >
                <el-option
                  v-for="item in item.selectOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </div>

            <div
              v-else-if="item.isSwitch"
              style="width: 100%"
            >
              <el-switch
                v-model="dialogDatas.currentData[item.value]"
                inline-prompt
                active-text="启用"
                inactive-text="不启用"
                :active-value="item.trueValue"
                :inactive-value="item.falseValue"
                :disabled="item.disable"
              />
            </div>
            <div
              v-else
              style="width: 100%"
            >
              <el-input
                v-model="dialogDatas.currentData[item.value]"
                :disabled="item.disable"
              />
            </div>
          </el-form-item>
        </el-form>
      </el-scrollbar>

      <template #footer>
        <span>
          <el-button @click="dialogDatas.dialogFormVisible = false">取消</el-button>
          <el-button
            type="primary"
            @click="dialogDatas.confirmBtnClick"
          >
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>

    <el-table
      class="marginTop10"
      :data="tableDatas.tableDatasAfterPage"
      stripe
      border
      empty-text="无数据"
      max-height="80vh"
    >
      <el-table-column
        v-for="column in tableDatas.tableColumns"
        :type="column.type"
        :prop="column.prop"
        :label="column.label"
        :align="column.align"
        :width="column.width"
        sortable
      >
        <template
          #default="scope"
          v-if="column.prop =='operate'"
        >
          <div>
            <el-button
              type="success"
              @click="tableDatas.editBtnClick(scope.row)"
            >修改</el-button>
            <el-popconfirm
              confirm-button-text="确定"
              cancel-button-text="取消"
              title="确定删除该条记录?"
              @confirm="tableDatas.deleteBtnClick(scope.row)"
            >
              <template #reference>
                <el-button type="danger">删除</el-button>
              </template>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- <el-pagination
      class="marginTop10"
      @size-change="tableDatas.filters.handleSizeChange"
      @current-change="tableDatas.filters.handleCurrentChange"
      :page-sizes="[10, 15, 20, 100]"
      :page-size="tableDatas.filters.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableDatas.tableDatasAfterFilter.length"
    >
    </el-pagination> -->

  </div>
</template>
<script>
import { computed, reactive, ref } from 'vue';
import { onMounted, watch } from '@vue/runtime-core';
import { v4 } from 'uuid';
import myAxios from '@/myAxios/myAxios';
import { showWarning } from '@/hooks/useMyMessageBox';
import useMyXlsx from '@/hooks/useMyXlsx';

export default {
  name: 'RANK_PView',
  setup () {
    let loading = ref(false);
    let addBtnClick = function () {
      showFormDialog();
    };
    let exportBtnClick = function () {
      let xlsx = useMyXlsx();
      let exPorps = ['', 'operate', 'P_ID'];
      let columns = tableDatas.tableColumns.filter(item => !exPorps.includes(item.prop));
      let titles = {};
      let header = [];
      for (let index = 0; index < columns.length; index++) {
        const element = columns[index];
        titles[element.prop] = element.label;
        header.push(element.prop);
      }
      let exportOption = {
        titles,
        header,
        hideColumns: ['P_ID'],
      };

      xlsx.exportDatas(tableDatas.oritableData, exportOption, '人员等级.xlsx');
    };

    let showFormDialog = function (row) {
      if (row) {
        dialogDatas.isNew = false;
        dialogDatas.currentData = { ...row };
      }
      else {
        dialogDatas.isNew = true;
        dialogDatas.currentData = {};
      }
      dialogDatas.dialogFormVisible = true;
    };

    let dialogDatas = reactive({
      dialogFormVisible: false,
      currentData: {},
      isNew: false,
      formItems: [
        // {
        //   "value": "P_ID",
        //   "label": "唯一ID",
        //   "isDate": false
        // },
        {
          "value": "NAME",
          "label": "姓名",
          "isDate": false
        },
        {
          "value": "RANK_NUM",
          "label": "岗位技能等级",
          "isDate": false
        },
        {
          "value": "DGBZ",
          "label": "定额标准",
          "isDate": false
        },
        {
          "value": "RANK",
          "label": "等级",
          "isDate": false,
          "disable": false,
          "isSelect": true,
          "selectOptions": [
            {
              "value": "A",
              "label": "A"
            },
            {
              "value": "B",
              "label": "B"
            },
            {
              "value": "C",
              "label": "C"
            },
            {
              "value": "D",
              "label": "D"
            },
          ],
        },
        {
          "value": "DEJZ",
          "label": "定额基数",
          "isDate": false
        },
        {
          "value": "P_TYPE",
          "label": "正式工/外包工/岗位工",
          "isDate": false,
          "disable": false,
          "isSelect": true,
          "selectOptions": [
            {
              "value": "正式工",
              "label": "正式工"
            },
            {
              "value": "外包工",
              "label": "外包工"
            },
            {
              "value": "岗位工",
              "label": "岗位工"
            },
          ],
        },
        // {
        //   "value": "EMP_DEPARTMENT",
        //   "label": "部门",
        //   "isDate": false,
        //   "disable": false,
        //   "isSelect": true,
        //   "selectOptions": [
        //     {
        //       "value": "value_1",
        //       "label": "label_1"
        //     },
        //     {
        //       "value": "value_2",
        //       "label": "label_2"
        //     }
        //   ],
        // },
        // {
        //   "value": "DATE_TIME",
        //   "label": "精确到秒的时间(示例,可删除)",
        //   "isDate": true,
        //   "dateOption": {
        //     "type": "datetime",
        //     "format": "YYYY/MM/DD HH:mm:ss",
        //     "dateFormat": "YYYY/MM/DD",
        //     "timeFormat": "HH:mm:ss",
        //     "valueFormat": "YYYY/MM/DD HH:mm:ss"
        //   },
        // },
      ],
      confirmBtnClick: function () {
        loading.value = true;

        let row = dialogDatas.currentData;
        let P_ID = row.P_ID ? row.P_ID : v4();

        let params = `${P_ID}`
          + `\t${row.NAME}`
          + `\t${row.RANK_NUM}`
          + `\t${row.DGBZ}`
          + `\t${row.RANK}`
          + `\t${row.DEJZ}`
          + `\t${row.P_TYPE}`;
        myAxios.post('陈帅廷添加或修改配送中心人员等级', params).then(res => {
          if (res.includes('成功')) {
            loading.value = false;
            dialogDatas.dialogFormVisible = false;
            ElMessage({
              showClose: true,
              type: 'success',
              message: '提交成功!' + res
            });
            getTableDatas();
          }
          else {
            loading.value = false;
            if (res.includes('违反唯一约束条件 (P100139TEST.CST_PSZX_P_RANK_U1)')) {
              showWarning('更新失败res,人员姓名已存在!请再次确认人员姓名!');
            }
            else {
              showWarning('更新失败res!' + res);
            }
          }
        }).catch((err) => {
          loading.value = false;
          showWarning('更新失败err!' + err);
        });

      },
      cancelBtnClick: function () {

      }
    });

    let getTableDatas = function () {
      loading.value = true;

      let params = ``;
      myAxios.get('陈帅廷查询配送中心人员等级', params).then(res => {
        if (res instanceof Array) {
          loading.value = false;
          tableDatas.oritableData = [...res];
          ElMessage({
            showClose: true,
            type: 'success',
            message: '最新数据获取成功!'
          });
        }
        else {
          loading.value = false;
          showWarning('获取数据失败res!' + res);
        }
      }).catch((err) => {
        loading.value = false;
        showWarning('获取数据失败err!' + err);
      });
    };

    onMounted(() => {
      getTableDatas();
    });

    let searchKeys = ['NAME', 'RANK_NUM', 'DGBZ', 'RANK', 'DEJZ', 'P_TYPE'];
    let tableDatas = reactive({
      //表格筛选展示相关代码
      filters: {
        searchText: '',

        //表格翻页相关代码
        currentPage: 1,
        pagesize: 10,
        handleSizeChange (val) {
          tableDatas.filters.pagesize = val;
        },
        handleCurrentChange (val) {
          tableDatas.filters.currentPage = val;
        },
      },
      tableDatasAfterFilter: computed(() => {
        let fData = tableDatas.oritableData;

        if (tableDatas.filters.searchText) {
          let searchText = tableDatas.filters.searchText;
          fData = fData.filter(item => {
            return searchKeys.some(key => {
              return String(item[key]).toLowerCase().includes(searchText.toLowerCase());
            });
          });
        }

        return fData;
      }),
      tableDatasAfterPage: computed(() => {
        let fData = tableDatas.tableDatasAfterFilter;
        // fData = fData.slice((tableDatas.filters.currentPage - 1) * tableDatas.filters.pagesize, tableDatas.filters.currentPage * tableDatas.filters.pagesize);
        return fData;
      }),

      tableColumns: [
        {
          "type": "index",
          "prop": "",
          "label": "序号",
          "align": "center",
          "width": 80
        },
        // {
        //   "prop": "P_ID",
        //   "label": "唯一ID",
        //   "align": "center"
        // },
        {
          "prop": "NAME",
          "label": "姓名",
          "align": "center"
        },
        {
          "prop": "RANK_NUM",
          "label": "岗位技能等级",
          "align": "center"
        },
        {
          "prop": "DGBZ",
          "label": "定额标准",
          "align": "center"
        },
        {
          "prop": "RANK",
          "label": "等级:ABCD",
          "align": "center"
        },
        {
          "prop": "DEJZ",
          "label": "定额基数",
          "align": "center"
        },
        {
          "prop": "P_TYPE",
          "label": "正式工/外包工/岗位工",
          "align": "center"
        },
        {
          "prop": "operate",
          "label": "操作",
          "align": "center",
          "width": 170
        }
      ],
      oritableData: [
      ],
      editBtnClick: function (row) {
        showFormDialog(row);
      },
      deleteBtnClick: function (row) {
        loading.value = true;

        let params = `${row.P_ID}`;
        myAxios.post('陈帅廷删除配送中心人员等级', params).then(res => {
          if (res.includes('成功')) {
            loading.value = false;
            dialogDatas.dialogFormVisible = false;
            ElMessage({
              showClose: true,
              type: 'success',
              message: '删除成功!' + res
            });
            tableDatas.oritableData.splice(tableDatas.oritableData.indexOf(row), 1);
          }
          else {
            loading.value = false;
            showWarning('删除失败res!' + res);
          }
        }).catch((err) => {
          loading.value = false;
          showWarning('删除失败err!' + err);
        });

      },
    });

    return {
      loading,
      addBtnClick,
      exportBtnClick,
      dialogDatas,
      tableDatas
    };
  }
}
</script>
<style lang="css" scope>
.marginTop10 {
  margin-top: 10px;
}
</style>

